package com.lehe.controller.admin;

import com.lehe.common.Contants;
import com.lehe.common.utils.StringUtil;
import com.lehe.entry.AdminMenuDto;
import com.lehe.entry.AdminUserSessionDto;
import com.lehe.entry.UserInfoDto;
import com.lehe.entry.UserSessionDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class BaseAdminController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected HttpSession session;

	protected AdminUserSessionDto getAdminUserSession() {
		Object o = session.getAttribute(Contants.SESSION_ADMIN_USER_KEY);
		if (null == o) {
			return null;
		} else {
			return (AdminUserSessionDto) o;
		}
	}

	protected Long getAdminUserRoleId(){
		Long roleId = -1L;
		AdminUserSessionDto adminUserSessionDto = getAdminUserSession();
		if(null!=adminUserSessionDto){
			roleId = adminUserSessionDto.getRoleId();
		}
		return roleId;
	}
	
	protected final Long getAdminUserId(){
		Long userId = -1L;
		AdminUserSessionDto adminUserSessionDto = getAdminUserSession();
		if(null!=adminUserSessionDto){
			userId = adminUserSessionDto.getId();
		}
		return userId;
	}
	
	protected final List<Long> getGameAgentId(){
		List<Long> agentIds = new ArrayList<>();
		AdminUserSessionDto adminUserSessionDto = getAdminUserSession();
		if(null!=adminUserSessionDto){
			List<UserInfoDto> userInfoDtoList = adminUserSessionDto.getUserInfoDtoList();
			if(null!=userInfoDtoList && !userInfoDtoList.isEmpty()){
				for(UserInfoDto user :userInfoDtoList){
					if(null!=user.getAgentId() && user.getAgentId() > 0){
						agentIds.add(user.getAgentId());
					}
				}
			}else {
				String userIds = adminUserSessionDto.getUserIds();
				if("-1".equals(userIds) && 1 != adminUserSessionDto.getRoleId()){
					agentIds.add(-1L);
				}
			}
		}
		return agentIds;
	}

	/**
	 * 后台用户关联的游戏玩家id，多个用逗号隔开
	 * @return
	 */
	protected List<Long> getGameUserIds(){
		List<Long> userIds = new ArrayList<>();
		AdminUserSessionDto adminUserSessionDto = getAdminUserSession();
		if(null!=adminUserSessionDto){
			List<UserInfoDto> userInfoDtoList = adminUserSessionDto.getUserInfoDtoList();
			if(null!=userInfoDtoList && !userInfoDtoList.isEmpty()){
				for(UserInfoDto user :userInfoDtoList){
					userIds.add(user.getUserId());
				}
			}else {
				String userIdsStr = adminUserSessionDto.getUserIds();
				if("-1".equals(userIdsStr) && 1 != adminUserSessionDto.getRoleId()){
					userIds.add(-1L);
				}
			}
		}
		return userIds;
	}

	protected List<AdminMenuDto> getUserMenus(){
		List<AdminMenuDto> ret = null;
		AdminUserSessionDto adminUserSessionDto = getAdminUserSession();
		if(null!=adminUserSessionDto){
			ret = adminUserSessionDto.getMenuDtoList();
		}
		return ret;
	}


}
